<template>
  <div class="login-setting">
    <el-form
      ref="userForm"
      :model="userInfo"
      :rules="rules"
      label-width="120px"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userInfo.username" style="width:300px" />
      </el-form-item>

      <el-form-item label="密码:" prop="password">
        <el-input v-model="userInfo.password" type="password" style="width:300px" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="undateUserinfo">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailById, updateUserInfoAPI } from '@/api/user'
export default {
  name: 'LoginSetting',
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      id: this.$route.query.id, // 当前用户id
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码为6-16位', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetailByIdFn()
  },
  methods: {
    // 根据id获取用户信息方法
    async  getUserDetailByIdFn() {
      const res = await getUserDetailById(this.id)
      this.userInfo = res.data
    },
    // 更改用户信息
    undateUserinfo() {
      this.$refs.userForm.validate(async valid => {
        if (!valid) return
        await updateUserInfoAPI(this.id, this.userInfo)
        this.getUserDetailByIdFn()
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
